package com.ks.comment.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.comment.dao.ZpzCommentMapper;
import com.ks.comment.entity.MemberBase;
import com.ks.comment.entity.ZpzComment;
import com.ks.comment.feign.IBCmsNewsService;
import com.ks.comment.feign.IMemberBaseService;
import com.ks.comment.service.IZpzCommentService;
import com.ks.comment.service._ServiceBase;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 信息点赞/踩日志表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-18
 */
@Service
public class ZpzCommentServiceImpl extends _ServiceBase<ZpzCommentMapper, ZpzComment> implements IZpzCommentService {
    @Resource
    private ZpzCommentMapper zpzCommentMapper;
    @Resource
    private IBCmsNewsService ibCmsNewsService;
    @Resource
    private IMemberBaseService baseService;


    @Override
    public int addComment(OperationContext operationContext, ZpzComment zpzComment) {
        zpzComment.setIuserId(operationContext.getUserId().intValue());
        zpzComment.setCreateUserID(operationContext.getUserId().intValue());
        zpzComment.setTenantID(operationContext.getTenantId().intValue());
        zpzCommentMapper.setUTF();
        Integer insert = zpzCommentMapper.insert(zpzComment);
        return insert;
    }

    @Override
    public Map<String, Object> getCommentList(OperationContext operationContext, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        Page page = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        EntityWrapper<ZpzComment> wrapper = new EntityWrapper<>();
        ZpzComment zpzComment = new ZpzComment();
        zpzComment.setTenantID(operationContext.getTenantId().intValue());
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        for (SearchCriteriaBase.matchField field : fields) {
            if ("informationId".equals(field.getName())) {
                zpzComment.setInformationId(Integer.valueOf(field.getValue()));
            }
            if ("type".equals(field.getName())) {
                zpzComment.setCType(field.getValue());
            }
        }
        zpzComment.setSStatus(Const.DEL_FLAG_DEL);
        wrapper.setEntity(zpzComment);
        wrapper.orderBy("createTime desc");
        Integer integer = zpzCommentMapper.selectCount(wrapper);
        map.put("count", integer);
        List<ZpzComment> zpzComments = zpzCommentMapper.selectPage(page, wrapper);
        if (ToolUtil.isNotEmpty(zpzComments)) {
            for (ZpzComment comment : zpzComments) {
                EntityWrapper<ZpzComment> wrapper1 = new EntityWrapper<>();
                ZpzComment comment1 = new ZpzComment();
                comment1.setIResultId(comment.getId());
                comment1.setSStatus(Const.DEL_FLAG_DEL);
                comment1.setCCommentType(Const.DEL_FLAG_DEL);
                comment1.setDelMark(Const.DEL_FLAG_NORMAL);
                comment1.setTenantID(operationContext.getTenantId().intValue());
                wrapper1.setEntity(comment1);
                List<ZpzComment> comments = zpzCommentMapper.selectList(wrapper1);
                if (ToolUtil.isNotEmpty(comments)){
                    for (ZpzComment zpzComment1 :comments){
                        MemberBase memberBase = baseService.queryMemberInfo(zpzComment1.getIuserId());
                        zpzComment1.setCommentName(memberBase.getINickName());
                        zpzComment1.setCommentImg(memberBase.getIfavatar());
                    }
                }
                MemberBase memberBase = baseService.queryMemberInfo(comment.getIuserId());
                comment.setCommentList(comments);
                comment.setCommentName(memberBase.getINickName());
                comment.setCommentImg(memberBase.getIfavatar());
            }
        }
        map.put("list", zpzComments);
        return map;
    }

    @Override
    public int updateCommentStatus(OperationContext operationContext, int commentId, int status) {
        ZpzComment zpzComment = zpzCommentMapper.selectById(commentId);
        if (ToolUtil.isNotEmpty(zpzComment)) {
            if (status ==Const.DEL_FLAG_DEL){
                ibCmsNewsService.addCmsCount(zpzComment.getInformationId(),Const.Cms.COMMENT_TYPE);
            }
            zpzComment.setSStatus(status);
            Integer integer = zpzCommentMapper.updateById(zpzComment);
            return integer;
        }
        return 0;
    }

    @Override
    public Map<String, Object> getAdminCommentList(int inFormationId, String type, int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page page = new Page(pageNo, pageSize);
        EntityWrapper<ZpzComment> wrapper = new EntityWrapper<>();
        ZpzComment zpzComment = new ZpzComment();
        zpzComment.setInformationId(inFormationId);
        zpzComment.setCType(type);
        wrapper.setEntity(zpzComment);
        Integer integer = zpzCommentMapper.selectCount(wrapper);
        map.put("count", integer);
        List<ZpzComment> zpzComments = zpzCommentMapper.selectPage(page, wrapper);
        if (ToolUtil.isNotEmpty(zpzComments)){
            for (ZpzComment zpzComment1 :zpzComments){
                MemberBase memberBase = baseService.queryMemberInfo(zpzComment1.getIuserId());
                zpzComment1.setCommentName(memberBase.getINickName());
                zpzComment1.setCommentImg(memberBase.getIfavatar());
            }
        }
        map.put("list", zpzComments);
        return map;
    }
}
